#pragma once
#include "Common.h"
#include "ThreadCache.h"
#include "PageCache.h"

static void* ConcurrenAlloc(size_t size)
{
	if (size > MAX_Bytes - 1)
	{
		size_t alignsize = Sizeclass::roundup(size);
		size_t kpage = alignsize >> PAGE_SHIFT;
        
		span* nspan = PageCache::newspan(kpage);
		void* ptr = (void*)(nspan->_pageid << PAGE_SHIFT);
		return ptr;
	}
	if (pTLSThreadCache == nullptr)
	{
		pTLSThreadCache = new ThreadCache;
	}
	cout << std::this_thread::get_id() << ":" << pTLSThreadCache << endl;
	return pTLSThreadCache->setAlloc(size);
}

static void ConcurrentFree(void* index, size_t size)
{
	if (size > MAX_Bytes)
	{
		span* span = PageCache::mapobjecttospan(index);
		PageCache::getinstance()->RealsespantoPage(span);

	}
	else
	{
		assert(pTLSThreadCache);
		ThreadCache::deAlloc(index, size);
	}
}